(function () {
    window.Ball = class Ball {
        constructor () {
            this.x = 0
            this.y = 0
            this.width = 24
            this.height = 24
            this.speed = 6
            this.type = 'normal'
            this.typeTimer = null
            this.sizeTimer = null
            this.angle = randomNum(0,1) ?  -randomNum(30,60) :  -randomNum(120,150)
        }
        update() {
            if (game.state === 0) {
                this.x = game.racket.x + game.racket.width / 2 - this.width / 2
                this.y = game.racket.y - this.height
            } else if (game.state === 1) {
                this.x += this.speed * Math.cos(this.angle * Math.PI / 180)
                this.y += this.speed * Math.sin(this.angle * Math.PI / 180)
                // 碰到左右边
                if (this.x <= 0 || this.x + this.width >= game.canvas.width) {
                    this.angle = 180 - this.angle
                } else if (this.y <= 0) {
                    // 碰到上边
                    this.angle = -this.angle
                } else if (this.y + this.height >= game.canvas.height) {
                    // 碰到下边
                    game.life.lose()
                    game.state = 0
                    this.angle = randomNum(0,1) ?  -randomNum(30,60) :  -randomNum(120,150)
                    
                } else if (this.x >= game.racket.x && this.x <= game.racket.x + game.racket.width && this.y + this.height >= game.racket.y) {
                    this.angle = -this.angle
                }
            }
        }
        reward (type) {
            if (type <= 5) return
            switch (type) {
                case 6:
                 if(this.type === 'fire') break;
                 this.type = 'fire';
                 this.typeTimer = setTimeout(() => {
                    this.type = 'normal'
                 },4500)
                break;
                case 7 :
                    if (this.width === 36) break;
                        this.width = 36
                        this.height = 36
                        this.sizeTimer = setTimeout(() => {
                        this.width = 24
                        this.height = 24
                    },10000)
                    break;
                case 8: 
                game.life.add()
                break;
                case 9:
                    if (this.speed >=8) break;
                this.speed ++
            }
        }
        reset () {
            this.x = 0
            this.y = 0
            this.type = 'normal'
            this.width = 24
            this.height = 24
            this.speed  = this.speed <= 6 ? 6 : this.speed --
            clearTimeout(this.typeTimer)
            clearTimeout(this.sizeTimer)
        }
        render () {
            this.type === 'normal'  ? game.context.drawImage(game.images.ball,this.x,this.y,this.width,this.height) : game.context.drawImage(game.images.ballFire,this.x,this.y,this.width,this.height)
        }
    }
})()